<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>barcode</title>
	<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
	<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
	<script src="src/layer/layer.js" type="text/javascript"></script>
</head>

<style type="text/css">
	input{
		width: 500px;height: 45px;border-radius: 1px solid #ddd;
		display: block;
		margin : 0 auto;
		padding: 0px 5px;
		outline: none;
	}
</style>
	<body>
		<div class="row">
			<div class="col-lg-12" style="text-align:center;margin-top: 16%">
				<h1>入场扫描</h1>
			</div>
			<div class="col-lg-12">
				<input id="barCode" value="" type="text" placeholder="stay focus" style="" name="">
			</div>
			<div class="col-lg-12">
				<div id="info" style="width: 500px;margin: 0 auto">

				</div>
			</div>
		</div>
		<script type="text/javascript" src="src/jquery.barcode.js"></script>
		<script type="text/javascript">
			$("#barCode").startListen({
				barcodeLen : 10,
				letter : true,
				number : true,
				check  : true,
				show : function(code){
					$('#info').append($('<h3/>').text(code))
					console.log('扫码结果:'+code)
				}
			});
			
			const sleep = (waitSeconds) => {
				return new Promise(resolve => {
					setTimeout(() => {
						resolve()
					}, waitSeconds)
				})
			}
			
			function getRandom(num){
				return Math.floor((Math.random()+Math.floor(Math.random()*9+1))*Math.pow(10,num-1))+'';
			}
			

			async function mock(){
				
				async function input(){
					var input = document.getElementById('barCode')
					console.log(input)
					var num = 'a'+getRandom(9)
					for(var i of num.split("")){
						var k = i.charCodeAt()
						console.log(k)
						
						fireKeyEvent(input,'keydown',k)
						await sleep(20)
						fireKeyEvent(input,'keypress',k)
						fireKeyEvent(input,'keyup',k)
						
					}
					fireKeyEvent(input,'keydown',13)
					await sleep(20)
					fireKeyEvent(input,'keypress',13)
					
					fireKeyEvent(input,'keyup',13)
				}
			
			
				await sleep(1000)
				
					for(var i=0;i<10;i++){
						console.log(`开始执行${i}第次`)
						await sleep(1000)	
						await input()
						console.log(`结束执行${i}第次`)	
					}
				
			}
			
			mock()
			
		</script>
	</body>
	
	<script>
		function fireKeyEvent(el, evtType, keyCode){
	var doc = el.ownerDocument,
		win = doc.defaultView || doc.parentWindow,
		evtObj;
	if(doc.createEvent){
		if(win.KeyEvent) {
			evtObj = doc.createEvent('KeyEvents');
			evtObj.initKeyEvent( evtType, true, true, win, false, false, false, false, keyCode, 0 );
		}
		else {
			evtObj = doc.createEvent('UIEvents');
			Object.defineProperty(evtObj, 'keyCode', {
		        get : function() { return this.keyCodeVal; }
		    });     
		    Object.defineProperty(evtObj, 'which', {
		        get : function() { return this.keyCodeVal; }
		    });
			evtObj.initUIEvent( evtType, true, true, win, 1 );
			evtObj.keyCodeVal = keyCode;
			if (evtObj.keyCode !== keyCode) {
		        console.log("keyCode " + evtObj.keyCode + " 和 (" + evtObj.which + ") 不匹配");
		    }
		}
		el.dispatchEvent(evtObj);
	} 
	else if(doc.createEventObject){
		evtObj = doc.createEventObject();
		evtObj.keyCode = keyCode;
		el.fireEvent('on' + evtType, evtObj);
	}
}

window.onload=function(){
	
	
}
	
	</script>
</html>



